<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <a-spin :spinning="isLoading">
      <a-form :form="form" @submit="handleSubmit">
<!--        <a-form-item label="推荐奖金设置" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--          <a-input v-decorator="['tjr_setting', {initialValue : record.tjr_setting}]" />-->
<!--          <div class="form-item-help">-->
<!--            <p class="extra">推荐奖金定额</p>-->
<!--          </div>-->
<!--        </a-form-item>-->
        <a-form-item label="签到参数设置" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['sign_setting', {initialValue : record.sign_setting}]" />
          <div class="form-item-help">
            <p class="extra">
              （7天为周期、每日签到积分以1-6积分递增、第7天可领15积分）
            </p>
          </div>
        </a-form-item>
        <a-form-item label="签到规则" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['content', {initialValue : record.content}]" />
          <div class="form-item-help">
            <p class="extra">
              （签到规则）
            </p>
          </div>
        </a-form-item>
        <a-form-item label="平级参数设置" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['equal_setting', {initialValue : record.equal_setting}]" />
          <div class="form-item-help">
            <p class="extra">
              （平级提成（仅限于品王（省代）之间的分成。假设A品王开展了B品王，B品王的消费以及收益A品王占%）
            </p>
          </div>
        </a-form-item>
        <a-form-item label="分享说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['share_content', {initialValue : record.share_content}]" />
        </a-form-item>
        <a-form-item
          label="权益说明"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          extra=""
        >
          <SelectImage
            :defaultList="record.logoImage ? [record.logoImage] : []"
            v-decorator="['logo_image_id']"
          />
        </a-form-item>
        <a-form-item
          label="分享海报"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          extra=""
        >
          <SelectImage
            :defaultList="record.codeImage ? [record.codeImage] : []"
            v-decorator="['code_image_id']"
          />
        </a-form-item>
        <a-form-item :wrapper-col="{ span: wrapperCol.span, offset: labelCol.span }">
          <a-button type="primary" html-type="submit">提交</a-button>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/setting/store'
import { isEmpty } from '@/utils/util'
import { SelectImage } from '@/components'

export default {
  components: {
    SelectImage
  },
  data () {
    return {
      // 当前设置项的key
      key: 'setting',
      // 标签布局属性
      labelCol: { span: 4 },
      // 输入框布局属性
      wrapperCol: { span: 10 },
      // loading状态
      isLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 单选框样式
      radioStyle: {
        display: 'block',
        height: '30px',
        lineHeight: '30px'
      },
      // 当前记录详情
      record: {}
    }
  },
  // 初始化数据
  created () {
    // 获取当前详情记录
    this.getDetail()
  },
  methods: {

    // 获取当前详情记录
    getDetail () {
      this.isLoading = true
      Api.detail(this.key)
        .then(result => {
          // 当前记录
          this.record = result.data.values
          // 设置默认值
          this.setFieldsValue()
        })
        .finally(result => {
          this.isLoading = false
        })
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { record, $nextTick, form } = this
      !isEmpty(form.getFieldsValue()) && $nextTick(() => {
        form.setFieldsValue(pick(record, ['order', 'freight_rule', 'logo_image_id']))
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
    * 提交到后端api
    */
    onFormSubmit (values) {
      this.isLoading = true
      Api.update(this.key, { form: values })
        .then((result) => {
          // 显示提示信息
          this.$message.success(result.message, 1.5)
        })
        .finally((result) => {
          this.isLoading = false
        })
    }

  }
}
</script>
<style lang="less" scoped>
/deep/.ant-form-item-control {
  padding-left: 10px;

  .ant-form-item-control {
    padding-left: 0;
  }
}
.ant-divider {
  margin-top: 60px !important;
}
.ant-input-number {
  width: 160px;
}
.input-text_right {
  margin-left: 10px;
}
.radio-item {
  margin-bottom: 16px;
}
</style>
